package com.util;

import lombok.Getter;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class ScreenUtil {
    public static final List<Screen> screenList = new ArrayList<>();

    static {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] screens = ge.getScreenDevices();
        for (GraphicsDevice screen : screens) {
            DisplayMode displayMode = screen.getDisplayMode();
            int screenSizeWidth = displayMode.getWidth();
            int screenSizeHeight = displayMode.getHeight();
            Rectangle screenBounds = screen.getDefaultConfiguration().getBounds();
            double scale = ((double) screenSizeWidth) / screenBounds.getWidth();
            System.out.println("屏幕分辨率：" + screenSizeWidth + "x" + screenSizeHeight);
            System.out.println("屏幕缩放比例：" + scale);
            System.out.println("缩放后分辨率：" + screenBounds.getWidth() + "x" + screenBounds.getHeight());
            screenList.add(new Screen(new Rectangle((int) screenBounds.getX(), (int) screenBounds.getY(), screenSizeWidth, screenSizeHeight), scale));
        }
    }

    public static Rectangle getScreenSize() {
        double x = Integer.MAX_VALUE;
        double y = Integer.MAX_VALUE;
        double w = Integer.MIN_VALUE;
        double h = Integer.MIN_VALUE;
        for (Screen screen : screenList) {
            x = Math.min(x, screen.getFbl().getX());
            y = Math.min(y, screen.getFbl().getY());
            w = Math.max(w, screen.getFbl().getX() + screen.getFbl().getWidth());
            h = Math.max(h, screen.getFbl().getY() + screen.getFbl().getHeight());
        }
        return new Rectangle((int) x, (int) y, (int) w, (int) h);
    }

    public static class Screen {
        private final Rectangle fbl;
        private final double scale;

        public Screen(Rectangle fbl, double scale) {
            this.fbl = fbl;
            this.scale = scale;
        }

        /**
         * 获取屏幕分辨率
         */
        public Rectangle getFbl() {
            return fbl;
        }

        public Rectangle getBounds() {
            Rectangle bounds = new Rectangle();
            bounds.setBounds((int) (fbl.getX() / scale), (int) (fbl.getY() / scale), (int) (fbl.getWidth() / scale), (int) (fbl.getHeight() / scale));
            return bounds;
        }
    }
}
